using System.Collections.Generic;
using System.Linq;

namespace WGT
{
    public abstract partial class Dialog
    {
        private readonly List<string[]> all = new List<string[]>();
        private int iNextId = 1;

        public string Theme;

        protected void Init()
        {
            Reset();
            Run(MODE.SHOW);
        }

        public int Width { get; set; }
        public int Height { get; set; }

        protected void Init(int w, int h, string theme){
            Width = w;
            Height = h;
            Theme = theme;
            Reset();
            Run(MODE.SHOW);
        }

        public abstract void View();

        protected void L(int id)
        {
            all.Add(new[]{"*", id.ToString()});
        }

        protected void S(string content, params object[] vals)
        {
            all.Add(new[] {"+", string.Format(content, vals)});
        }

        protected void U(string content, params object[] vals)
        {
            all.Add(new[] {"", string.Format(content, vals)});
        }

        protected void UP(string id, string _attrs)
        {
            if (!string.IsNullOrEmpty(_attrs))
            {
                string js = _attrs
                    .TrimStart(' ').Split(' ')
                    .Aggregate(string.Empty, (current, pair) => current + string.Format(@"$('{0}').{1};", id, pair));
                U(js);
            }
        }

        protected void E(string id)
        {
            all.Add(new[] {"-", id});
        }

        public List<string[]> Response()
        {
            if (all.Count == 0) View();

            var result = new List<string[]>();
            result.AddRange(all);
            all.Clear();
            return result;
        }

        public void Reset()
        {
            iOld = 0;
            oReset();
            mode = MODE.SHOW;
        }
    }
}